package org.cresource.pet.service.impl;

import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.pet.domain.Pet;
import org.cresource.pet.domain.PetDetail;
import org.cresource.pet.mapper.PetDetailMapper;
import org.cresource.pet.mapper.PetMapper;
import org.cresource.pet.service.IPetService;
import org.cresource.product.domain.Product;
import org.cresource.product.domain.ProductDetail;
import org.cresource.user.domain.User;
import org.cresource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public PetDetail getDetailByPetId(Long petId) {
        return petDetailMapper.loadByPetId(petId);
    }

    @Override
    public void adopt(Long petId, Long id) {
        Pet pet = petMapper.loadById(petId);
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //2.绑定用户
        User user = userMapper.loadByloingInfoId(id);
        pet.setUser(user);
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        //3.@TODO 生成领养订单  + 支付
        System.out.println("领养成功！");
    }

    /**
     * 宠物添加
     * 宠物添加
     * 宠物添加
     */
    @Override
    public void add(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        if (detail!=null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }
    /**
     * 修改
     * 修改
     * 修改
     */
    @Override
    public void update(Pet pet) {
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        if (detail.getId()!=null){
            System.out.println(pet);
            petDetailMapper.update(pet.getDetail());
        }else {
            petDetailMapper.save(detail);
        }
        //在添加detail
        petMapper.update(pet);
    }

    /**
     * 上下架
     * 上下架
     * 上下架
     */
    @Override
    public void onoroffsale(Map<String,Object> map) {
        //flag为1为上架 0为下架
        Map<String,Object> map1 = new HashMap<>();
        map.put("ids",map.get("ids"));
        Integer flag = (Integer)map.get("type");
        if (flag == 1){
            map.put("state",flag);
            map.put("onsaletime",new Date());
            petMapper.onsale(map);
        }else{
            map.put("state",flag);
            map.put("offsaletime",new Date());
            petMapper.offsale(map);
        }
    }

}
